﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace EOrm
{
    /// <summary>
    /// where条件表达式访问类
    /// </summary>
    public abstract class WhereVisitor
    {
        /// <summary>
        /// 变量值栈
        /// </summary>
        protected Stack<object> _paramValStack = new Stack<object>();

        /// <summary>
        /// where条件栈
        /// </summary>
        protected Stack<string> _whereStringStack = new Stack<string>();

        /// <summary>
        /// 被not修饰的数量
        /// </summary>
        protected int _notToDeal = 0;

        /// <summary>
        /// 上一子句是否为not子句
        /// </summary>
        protected bool _isLastNot = false;

        /// <summary>
        /// where查询串片段
        /// </summary>
        protected string _whereStr = "";
        /// <summary>
        /// 访问表达式
        /// </summary>
        /// <param name="exp">要访问的表达式</param>
        /// <param name="table">目标表</param>
        public abstract void Visit(Expression exp, ITable table);

        /// <summary>
        /// where条件表达式合并
        /// </summary>
        /// <returns>合并结果</returns>
        public abstract string Combine();

        /// <summary>
        /// 转换表达式类型
        /// </summary>
        /// <param name="expType">表达式类型</param>
        /// <returns>对应的sql语句组成部分</returns>
        protected string ChangeExpressionType(ExpressionType expType)
        {
            switch (expType)
            {
                case ExpressionType.AndAlso:
                    return "AND";
                case ExpressionType.OrElse:
                    return "OR";
                case ExpressionType.Equal:
                    return "=";
                case ExpressionType.GreaterThan:
                    return ">";
                case ExpressionType.GreaterThanOrEqual:
                    return ">=";
                case ExpressionType.LessThan:
                    return "<";
                case ExpressionType.LessThanOrEqual:
                    return "<=";
                case ExpressionType.NotEqual:
                    return "<>";
                default:
                    Console.WriteLine(string.Format("表达式类型：{0}没有转换的必要", expType.ToString()));
                    return null;
            }
        }
    }
}
